package com.csu.letaobackend.serviceM.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csu.letaobackend.bean.Picture;
import com.csu.letaobackend.bean.Seller;
import com.csu.letaobackend.bean.Shop;
import com.csu.letaobackend.bean.User;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.PictureDao;
import com.csu.letaobackend.persistence.SellerDao;
import com.csu.letaobackend.persistence.ShopDao;
import com.csu.letaobackend.persistence.UserDao;
import com.csu.letaobackend.serviceM.SellerMService;
import com.csu.letaobackend.vo.SellerVO;
import com.csu.letaobackend.vo.UserVO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@MapperScan("com.csu.letaobackend.persistence")
public class SellerMServiceImpl implements SellerMService {

      @Autowired
      SellerDao sellerDao;

      @Autowired
      PictureDao pictureDao;

      @Autowired
      ShopDao shopDao;

      @Override
      public CommonResponse<IPage<SellerVO>> getAllSellers(int pageid, int pagesize) {
            IPage<Seller> sellerIPage = new Page<>(pageid, pagesize);
            sellerDao.selectPage(sellerIPage, null);

            List<SellerVO> sellerVOList = new ArrayList<>();
            for (Seller seller : sellerIPage.getRecords()) {
                  sellerVOList.add(entityToVO(seller));
            }
            IPage<SellerVO> sellerVOIPage= new Page<>(pageid, pagesize);
            sellerVOIPage.setTotal(sellerIPage.getTotal());
            sellerVOIPage.setRecords(sellerVOList);

            return CommonResponse.createForSuccess(sellerVOIPage);
      }

      @Override
      public CommonResponse<IPage<SellerVO>> searchSeller(String username, int pageid, int pagesize) {
            IPage<Seller> sellerIPage = new Page<>(pageid, pagesize);

            QueryWrapper<Seller> queryWrapper = new QueryWrapper<Seller>();
            queryWrapper.like("username",username);
            sellerDao.selectPage(sellerIPage, queryWrapper);

            List<SellerVO> sellerVOList = new ArrayList<>();
            for (Seller seller : sellerIPage.getRecords()) {
                  sellerVOList.add(entityToVO(seller));
            }
            IPage<SellerVO> sellerVOIPage= new Page<>(pageid, pagesize);
            sellerVOIPage.setTotal(sellerIPage.getTotal());
            sellerVOIPage.setRecords(sellerVOList);

            return CommonResponse.createForSuccess(sellerVOIPage);
      }

      @Override
      public CommonResponse updateAvailable(String username, int available) {
            UpdateWrapper<Seller> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("username",username).set("available",available);
            sellerDao.update(null,updateWrapper);
            return CommonResponse.createForSuccess();
      }

      @Override
      public CommonResponse deleteSeller(String username) {
            Seller seller= sellerDao.selectById(username);

            if(seller==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            else{
                  sellerDao.deleteById(username);
            }
            return CommonResponse.createForSuccessMessage("删除成功");
      }

      private SellerVO entityToVO(Seller seller) {
            SellerVO sellerVO = new SellerVO();
            sellerVO.setUsername(seller.getUsername());
            sellerVO.setPassword(seller.getPassword());
            sellerVO.setPhone(seller.getPhone());
            sellerVO.setEmail(seller.getEmail());
            sellerVO.setPictureid(seller.getPictureid());
            Picture picture = pictureDao.selectById(seller.getPictureid());
            sellerVO.setPictureSrc(picture.getSrc());
            sellerVO.setAvailable(seller.getAvailable());
            QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sellername",seller.getUsername());
            sellerVO.setShopnum(shopDao.selectList(queryWrapper).size());
            return sellerVO;

      }
}
